//
//  CustomAlterView.swift
//  Billing
//
//  Created by Dale on 17/4/20.
//  Copyright © 2017年 Nari. All rights reserved.
//

import UIKit


struct TypeModel {
    var title: String
    var isSelect: Bool
}

enum DatePickerType : Int{
    case DatePickerTypeDate = 0
    case DatePickerTypeTime = 1
}

typealias TypeClosure = (String)->Void

class CustomAlterView: UIView {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var messageLB: UILabel!
    var bgView: UIView!
    var typeClosure: TypeClosure!
    var datePickerType: DatePickerType!
    var tmpStr: String?
    var tmpType: String?
    var cellType: Int?
    var tmpArr:[Model]!
    
    @IBOutlet weak var confBtn: UIButton!
    
    
    lazy var dataSource = [
        TypeModel(title: "早上", isSelect: false),
        TypeModel(title: "中午", isSelect: false),
        TypeModel(title: "晚上", isSelect: false),
        TypeModel(title: "其它", isSelect: false)
    ]
    /*
     // Only override draw() if you perform custom drawing.
     // An empty implementation adversely affects performance during animation.
     override func draw(_ rect: CGRect) {
     // Drawing code
     }
     */
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.layer.cornerRadius = 5.0
        self.layer.masksToBounds = true
        self.datePicker?.addTarget(self, action: #selector(handleDatePicker(sender:)), for: .valueChanged)
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.reloadData()
    }
    
    
    func handleTap() -> Void {
        self.hideAlertView()
    }
    
    func handleDatePicker(sender: UIDatePicker) -> Void {
        pickDateAndTime()
    }
    
    func pickDateAndTime() -> Void {
        let dateFormatter = DateFormatter()
        if self.datePickerType == DatePickerType(rawValue: 0) {
            dateFormatter.dateFormat = "yyyy-MM-dd"
        } else {
            dateFormatter.dateFormat = "HH:mm"
        }
        print(dateFormatter.string(from: self.datePicker.date))
        self.tmpStr = dateFormatter.string(from: self.datePicker.date)
    }
    
    func showAlertView() -> Void {
        self.bgView = UIView(frame: UIScreen.main.bounds)
        self.bgView.backgroundColor = UIColor.black
        self.bgView.alpha = 0.0
        
        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(handleTap))
        self.bgView?.addGestureRecognizer(tap)
        
        UIApplication.shared.keyWindow?.addSubview(self.bgView)
        UIApplication.shared.keyWindow?.addSubview(self)
        self.center = self.bgView.center
        self.transform = self.transform.scaledBy(x: 0.01, y: 0.01);
        UIView.animate(withDuration: 0.3) {
            self.transform = .identity
            self.bgView.alpha = 0.4;
        }
    }
    
    func hideAlertView() -> Void {
        self.removeFromSuperview();
        self.bgView.removeFromSuperview();
    }
    
    //弹出确定类型
    @IBAction func handleConfirm(_ sender: Any) {
        self.typeClosure?("")
        self.hideAlertView()
    }
    
    @IBAction func hanldeCancle(_ sender: Any) {
        self.hideAlertView()
    }
    
    //选择确定类型
    @IBAction func handleConfirmBtnAction(_ sender: Any) {
        pickDateAndTime()
        self.typeClosure?(self.tmpStr!)
        self.hideAlertView()
    }
    @IBAction func handleCancleBtnAction(_ sender: Any) {
        self.hideAlertView()
    }
}

extension CustomAlterView: UITableViewDataSource {
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if cellType == 0 {
            let cellIdentifier = "cellIdentifier"
            var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: cellIdentifier)
                cell?.tintColor = UIColor(red: 28.0/255, green: 202.0/255, blue: 147.0/255,
                                          alpha: 1)
            }
            let typeModel = dataSource[indexPath.row]
            
            cell?.textLabel?.text = typeModel.title
            if typeModel.title == tmpType || typeModel.isSelect {
                cell?.accessoryType = .checkmark
            } else {
                cell?.accessoryType = .none
            }
            return cell!
        } else {
            let model = tmpArr?[indexPath.row]
            let cell = BGSubmitCell.cellWithTableView(tableView: tableView, obj: model as Any)
            cell.configureCell(model: model!)
            return cell
        }
        
    }
}

extension CustomAlterView: UITableViewDelegate {
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if cellType == 0 {
            let typeModel = dataSource[indexPath.row]
            tmpType = typeModel.title
            self.tableView.reloadData()
            
            self.typeClosure?(typeModel.title)
            self.hideAlertView()
        }
        
    }
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
}




